import api from '../../utils/api.js'
const app = getApp()
Page({
  data: {
    btnSelet: ['操作员', '司机'],
    btnIndex: 0,
    driver: '',
    jobId: '',
    password: ''
  },
  handleChange: function (e) {
    this.setData({ btnIndex: e.target.id, driver: '', jobId: '', password: '' })

  },
  handleBinding: function () {
    let data = {}
    let that = this;
    if (this.data.btnIndex == 0) {
      data = {
        User: this.data.jobId,
        password: this.data.password
      }
      if (that.data.jobId == '' || !that.data.jobId) {
        return wx.showToast({ title: '请输入登录工号!', icon: 'none' })
      }
      if (that.data.password == '' || !that.data.password) {
        return wx.showToast({ title: '请输入登录密码!', icon: 'none' })
      }
    } else if (this.data.btnIndex == 1) {
      data = {
        User: this.data.driver,
        password: this.data.jobId
      }
      if (that.data.driver == '' || !that.data.driver) {
        return wx.showToast({ title: '请输入登录的司机!', icon: 'none' })
      }
      if (that.data.jobId == '' || !that.data.jobId) {
        return wx.showToast({ title: '请输入登录工号!', icon: 'none' })
      }
    }
    wx.showLoading()
    wx.login({
      success (res) {
        api.Login({ system_id: 1140, code: res.code }).then((result) => {
          api.Binding({ ...data, Transaction_No: result.root.data.row[0].Transaction_No }).then((res) => {
            if (res.root.response.errcode == 0 && res.root.data.row.length != 0) {
              wx.hideLoading()
              //绑定后自动登录
              api.Login({ ...data, system_id: 1140 }).then((res) => {
                if (res.root.response.errcode == 0 && res.root.data.row.length != 0) {
                  const perData = {
                    userName: res.root.data.row[0].user_name,
                    userId: res.root.data.row[0].user_id,
                    token: res.root.data.row[0].token
                  }
                  wx.setStorageSync('personalInfo', perData)
                  wx.showToast({
                    title: '绑定成功,为您自动登录',
                    duration: 2000,
                    icon: 'none',
                    complete: () => {
                      setTimeout(() => {
                        wx.reLaunch({
                          url: '/repairPages/service_menu/service_menu'
                        })
                      }, 1500)
                    }
                  });
                }
              })
            } else {
              wx.showToast({
                title: res.root.response.errmsg,
                duration: 2000,
                icon: 'none'
              });
            }
          })
        })
      }
    })
  },
})
